from datetime import datetime

from flask import Blueprint, jsonify
from flask import request
from db import db
long_blue = Blueprint('long', __name__)


@long_blue.route('/long', methods=['POST'])
def long():
    name = request.form.get('name')

    sql = 'select zi.name,zi.url,user.id from user inner join jie on user.jie_id=jie.id inner join jie_zi on jie.id=jie_zi.jie_id inner join zi on jie_zi.zi_id=zi.id where user.name="%s"' % (name)
    res = db.find_all(sql)
    list = []
    for i in res:
        list.append(i)
    return jsonify({
        'code': 200,
        'list': list
    })


@long_blue.route('/qing', methods=['POST'])
def qing():
    stime = request.form.get('stime')
    etime = request.form.get('etime')
    days = request.form.get('days')
    type = request.form.get('type')
    nextid = request.form.get('nextid')
    userid = request.form.get('userid')

    sql = 'insert into qing (stime,etime,days,type,nextid,userid) values (%s,%s,%d,%d,%d,%d)' % (stime,
                                                                                                 etime, days, type,
                                                                                                 nextid, userid)
    db.update(sql)
    db.commit()

    return jsonify({
        'code': 200,
        'msg': "提交成功"
    })


@long_blue.route('/shen', methods=['post'])
def shen():

    status = request.form.get('status')

    if status == 1:
        sql = "update qing set status=2,nextid=0 where id=%d" % (id)
    else:
        sql = "select userid from qing where id=%d"%(id)
        res = db.find_all(sql)
        nextid = 0
        for i in res:
            nextid = i[0]
        sql = "update qing set status=3,nextid=%d where id=%d" % (nextid, id)
    db.find_all(sql)
    return jsonify({"code": 200})


@long_blue.route('/echarts')
def echarts():
    userid = int(request.args.get('userid'))
    month = datetime.strftime(datetime.now(), "%m")

    datelist = []
    salarylist = []

    # 查看用户的工资

    sql = 'select solening from user where id=%d' % (userid)
    rr = db.find_all(sql)
    solening = 0
    for i in rr:
        solening = i['solening']

    # 每天的工资
    day_solening = solening/21

    for i in range(3):
        nmonth = int(month) - i

        stime = datetime.strftime(datetime.now(), "%Y-" + str(nmonth) + '-01')
        list = [4, 6, 9, 11]
        if int(nmonth) in list:
            etime = datetime.strftime(datetime.now(), "%Y-" + str(nmonth) + '-30')

        else:
            etime = datetime.strftime(datetime.now(), "%Y-" + str(nmonth) + '-31')

        dmonth = datetime.strftime(datetime.now(), "%Y-" + str(nmonth))
        datelist.append(datetime.strftime(datetime.now(), "%Y-" + str(dmonth)))

        sql = "SELECT type,sum(days) as days from qing where userid='%d' and stime<='%s' and etime>='%s' group by type" % \
              (int(userid), etime, stime)
        res = db.find_all(sql)

        money = solening

        for i in res:
            print(i['type'],"??????????????????????????????????????????????????//")
            if i['type'] == 1:
                money = float(solening) - float(day_solening) * float(i['days'])
        salarylist.append(int(money))
    return jsonify({"code": 200, 'datelist': datelist, 'salarylist': salarylist})